﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Taurus.Garden.DTO;
using Taurus.Platform.BLL;
using Taurus.Platform.DTO;
using HtmlAgilityPack;
using Taurus.Platform.Net;
using System.Xml.Linq;
using Taurus.Garden.XML;

namespace Taurus.Garden.BLL
{
    /// <summary>
    /// 种子市场
    /// </summary>
    class SeedMarket
    {
        #region field

        private static SeedMarket _sm = null;
        private const string SEED_LIST_URL = "http://www.kaixin001.com/!house/!garden/seedlist.php?verify={0}";
        private SeedPersistence _sp;
        private string _verify = string.Empty;
        private UserAccount _ua;
        private List<Seed> _seeds;

        #endregion

        #region construction

        private SeedMarket(string verify,UserAccount ua)
        {
            _verify = verify;
            _ua = ua;
            _sp = new SeedPersistence();
            _seeds = GetSeedByXML();
        }

        #endregion

        #region single

        public static SeedMarket BuildInstance(string verify,UserAccount ua)
        {
            if (_sm == null)
            {
                _sm = new SeedMarket(verify, ua);
            }
            return _sm;
        }

        #endregion

        #region private method

        /// <summary>
        /// 从服务器获取种子列表
        /// </summary>
        /// <returns></returns>
        private List<Seed> GetSeedByServer()
        {
            HtmlDocument doc = Util.AccessPage(_ua, string.Format(SEED_LIST_URL, _verify));
            XDocument xdoc = XDocument.Parse(doc.DocumentNode.InnerHtml);
            List<Seed> list = new List<Seed>();
            foreach (var item in xdoc.Root.Element("seed").Elements("item").ToList())
            {
                Seed seed = new Seed();
                seed.Count = int.MaxValue;
                seed.ID = item.Element("seedid").Value;
                seed.IsKaiXinBi = item.Element("beannum").Value != "0";
                seed.Price = seed.IsKaiXinBi ? item.Element("beannum").Value : item.Element("price").Value;
                seed.Name = item.Element("name").Value;
                list.Add(seed);
            }
            return list;
        }

        private List<Seed> GetSeedByXML()
        {
            DateTime time = _sp.FileRecordTime(null);
            //不是今天更新的
            if (time.ToShortDateString()!=DateTime.Now.ToShortDateString())
            {
                foreach (var seed in GetSeedByServer())
                {
                    _sp.Update(seed);
                }
                _sp.FileRecordTime(DateTime.Now);
                _sp.Save();
            }
            return _sp.GetList(null);
        }

        #endregion

        #region public method

        public Seed GetSeed(string id)
        {
            return _seeds.Find(s => s.ID == id);
        }

        public Seed AddSeed(string id, string name)
        {
            Seed seed = new Seed() { ID = id, Name = name, IsKaiXinBi = false };
            _sp.Insert(seed);
            _seeds.Add(seed);
            _sp.Save();
            return seed;
        }

        #endregion

    }
}
